#############################################################################
#  Copyright (C) 2007 Boscovich, Maximiliano - mboscovich@gmail.com          #
#                                                                            #
#  This program is free software; you can redistribute it and/or modify      #
#  it under the terms of the GNU General Public License as published by      #
#  the Free Software Foundation; either version 3 of the License, or         #
#  (at your option) any later version. See tht GPL_Licence.txt for details.  #
#                                                                            #
#  This program is distributed in the hope that it will be useful,           #
#  but WITHOUT ANY WARRANTY; without even the implied warranty of            #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the              #
#  GNU General Public License for more details.                              #
#                                                                            #
##############################################################################

import sys,os
sys.path.append("../")

from tables import *
from packages import *
import logModule

class database:
    """
    database(path,db_name,db_engine)
    Where 
    - path: is the path to db (if it's a sqlite db)
    - db_name: is the database name
    - db_engine: is a engine, for example sqlite://, postgres://,mysql://
    """
    def __init__(self, path='/tmp', db_name='/cfpackage.db', db_engine='sqlite://'):
        db_filename = os.path.abspath(path + db_name)
        exists_db = os.path.exists(db_filename)
        connection_string = db_engine + db_filename
        connection = connectionForURI(connection_string)
        sqlhub.processConnection = connection
        if not exists_db:
            logModule.log("Creando la estructura de la Base de Datos")
            Group.createTable()
            Distribution.createTable()
            Architecture.createTable()
            Office.createTable()
            Template.createTable()
            Host.createTable()
            Package.createTable()
            TemplatePackageMustHave.createTable()
            TemplatePackageMayNotHave.createTable()
            TemplatePackageMayHave.createTable()
            HostPackageMustHave.createTable()
            HostPackageMayHave.createTable()
            HostPackageMayNotHave.createTable()
            apt_db = packages()
            apt_db.refreshPackagesAvaible()         

    def read_from_config(self):
        print "debe leer del archivo de conf y devolver el path, el nombre de la base\
                y el tipo de base de datos a utilizar"
        return 1
